﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace EMS
{
    public partial class Prices : DevExpress.XtraEditors.XtraForm
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        List<Entities.Type> lst = new List<Entities.Type>();
        string connectionString = ConfigurationManager.ConnectionStrings["OrlcConnect"].ConnectionString;
        public Prices()
        {
            InitializeComponent();
        }

        private void Prices_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Prices_Load(object sender, EventArgs e)
        {
            lst.Clear();
            var connection = new OracleConnection(connectionString);
            try
            {
                connection.Open();
                var cmd = new OracleCommand()
                                {
                                    Connection = connection,
                                    CommandText = "SELECT * FROM Type",
                                    CommandType = CommandType.Text
                                };

                OracleDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Entities.Type record = new Entities.Type()
                    {
                        TypeId = (int)reader.GetOracleNumber(0),
                        Name = reader.GetString(1),
                        Price = reader.GetFloat(2),
                        Description = reader.GetString(3)
                    };
                    lst.Add(record);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            lueDemo.Properties.DataSource = lst;
            lueDemo.Properties.DisplayMember = "Name";
            lueDemo.Properties.ValueMember = "TypeId";
            lueDemo.EditValue = 1;
        }

        private void lueDemo_EditValueChanged(object sender, EventArgs e)
        {
            Entities.Type record = (Entities.Type)lueDemo.Properties.GetDataSourceRowByDisplayValue(lueDemo.Text);
            txtDes.Text = record.Description;
            txtPrice.Text = record.Price.ToString();
        }

        private void llbEdit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Entities.Type record = (Entities.Type)lueDemo.Properties.GetDataSourceRowByDisplayValue(lueDemo.Text);
            using (var connection = new OracleConnection(connectionString))
            {
                connection.Open();
                OracleTransaction trans = connection.BeginTransaction();
                var cmd = new OracleCommand()
                {
                    Connection = connection,
                    CommandText = String.Format("UPDATE TYPE SET PRICE = '{0}' WHERE TYPEID = '{1}'", txtPrice.Text, record.TypeId),
                    CommandType = CommandType.Text,
                    Transaction = trans
                };
                try
                {
                    cmd.ExecuteNonQuery();
                    trans.Commit();
                    MessageBox.Show("Update successfully");
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    MessageBox.Show(ex.Message);
                }
            }
            
            Prices_Load(sender,e);
        }
    }
}
